import {Component} from '@angular/core';
import {App, NavController} from 'ionic-angular';
import {CityPickerService} from "../../../../provider/city-picker";

//导入三级跳转源页面

//actionsheet需要导入的组件
import {Platform} from 'ionic-angular';
//接受从上个页面传递过来的数据
import {NavParams} from 'ionic-angular';
import {Http} from '@angular/http';
import {interfaceUrls} from '../../../../provider/serverUrls'
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/timeout';

//产品信息页面需要引入的模块
import {ModalController} from 'ionic-angular';

//商品信息页面
import {productPage} from './productPage/productPage';
//导入服务中的产品信息详情
import {services} from '../../../../providers/service';
import {AppPopup} from "../../../../util/popup";
import {UserService} from "../../../../providers/user-service";
import {UserLogin} from "../../../../modules/app-user/user-login/user-login";

@Component({
    selector: 'page-inexistDealer', templateUrl: 'inexistDealer.html'
})
export class inexistDealerPage {
    cityData: any[]; //城市数据
    cityName: string = '请选择您的城市'; //初始化城市名
    code: string; //城市编码
    areadata: any;
    data: any;
    product: any;

    companyName;     //公司名称
    textareaInfo;       //详细地址信息
    remarks;        //对代理产品进行备注
    trenchName;
    trenchMobile;

    companyAddressId;
    param = {};


    isShow: boolean;
    isNeed: boolean;

    constructor(public navCtrl: NavController,
                public platform: Platform,
                private navParams: NavParams,
                private popup: AppPopup,
                private userServ: UserService,
                public http: Http,
                private app: App,
                public modalCtrl: ModalController,
                public services: services)
	    {
            // this.setCityPickerData();

            //接收个人信息和电话
            this.trenchName = this.navParams.get('trenchName');
            this.trenchMobile = this.navParams.get('trenchMobile');
            console.info(this.trenchName);

            this.cityData = this.services.cityData;      //获取城市信息
        };

    ionViewWillEnter() {
        this.userServ.getPersonInfo()
            .then(() => {
            })
            .catch(err => {
                console.error("用户详情获取失败：", err);
                if (err.errorMassage == "未登录或登录超时！") {
                    this.popup.alert(err.errorMassage)
                        .then(() => {
                            this.app.getRootNav().setRoot(UserLogin);
                        });
                }
            });
    }

    /**
     * 城市选择器被改变时触发的事件
     * @param event
     */
    cityChange(event) {
        this.areadata = event;
        console.log(event, this.areadata);
        this.code = event['region'].value;
        console.info("??????本身事件触发的查看code+", this.code)
    }

    addBack() {
        this.navCtrl.pop();
    }

    addNext() {

    }

    //选择代理酒产品的菜单


    openModal(characterNum) {

        let modal = this.modalCtrl.create(productPage, {product: this.product});
        modal.present();
    }

    //当前页面取消按钮的方法
    cancel() {
        this.navCtrl.popToRoot();
    }

    //确定按钮的方法
    confirm() {
        this.isShow = true;
    }

    cancelAdd() {
        console.info("模态框消失")
        this.isShow = false;
    }

    //把新建的数据传给后台
    correctAdd() {
        //在这里进行后端传值             需要注意data.name       data.mobile 也需要传给后端
        if (!this.areadata || !this.services.protectId) {
            this.isNeed = true;
        } else {
            this.isNeed = false;
            this.http
                .post(interfaceUrls.dealerAdd, this.param = {
                    "companyAddressId": "0," + this.areadata.province.value + "," + this.areadata.city.value + "," + this.areadata.region.value, //这里讲城市的ID值传过去是用一串还是怎么样
                    "companyName": this.companyName, "companyAddressName": this.textareaInfo,
                    "mobile": this.trenchMobile,
                    "name": this.trenchName,
                    "remarks": this.remarks,
                    "productId": this.services.protectId
                })
                .timeout(4000)
                .toPromise()
                .then((response) => {
                    console.info(this.param);
                    console.info(response);
                    let data = response.json();
                    if(data.resultStatus == "success") {
                        this.navCtrl.popToRoot();       //确定之后将数据添加到经销商页面
                    }else {
                        this.popup.alert(data.errorMassage);
                    }
                })
                .catch((err) => {
                    console.info(err)
                });
        }
    }


}




